package com.meihua.shellapp.activity.maker;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.BaseBean;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.databean.CustomerDataBean;
import com.meihua.shellapp.bean.CustomerDetailsBean;
import com.meihua.shellapp.dialog.SlideListDialog;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.MyOnclickEditText;

import java.util.Calendar;

import butterknife.Bind;

/**
 * 客户管理详情
 * 作者：lc on 2017/9/30.
 * 邮箱：lc0607live@163.com
 */

public class CustomerDetailsActivity extends BaseActivity {

    @Bind(R.id.iv_top_left)
    ImageView iv_top_left;//返回
    @Bind(R.id.tv_title)
    TextView tv_title;//标题
    @Bind(R.id.iv_top_right)
    ImageView iv_top_right;//编辑
    @Bind(R.id.iv_top_right_left)
    ImageView iv_top_right_left;//删除
    @Bind(R.id.tv_top_right)
    TextView tv_top_right;//保存

    @Bind(R.id.et_name)
    MyOnclickEditText et_name;//姓名
    @Bind(R.id.et_sex)
    MyOnclickEditText et_sex;//性别
    @Bind(R.id.et_birthday)
    MyOnclickEditText et_birthday;//生日
    @Bind(R.id.et_document_type)
    MyOnclickEditText et_document_type;//证件类型
    @Bind(R.id.et_document_number)
    MyOnclickEditText et_document_number;//证件号码
    @Bind(R.id.et_marital_status)
    MyOnclickEditText et_marital_status;//婚姻状态
    @Bind(R.id.et_work_unit)
    MyOnclickEditText et_work_unit;//工作单位
    @Bind(R.id.et_phone)
    MyOnclickEditText et_phone;//手机号
    @Bind(R.id.et_email)
    MyOnclickEditText et_email;//邮箱
    @Bind(R.id.et_zip_code)
    MyOnclickEditText et_zip_code;//邮政编码
    @Bind(R.id.rl_address)
    RelativeLayout rl_address;//联系地址rl
    @Bind(R.id.et_address_details)
    MyOnclickEditText et_address_details;//详细地址
    @Bind(R.id.et_address)
    MyOnclickEditText et_address;//联系地址
    @Bind(R.id.et_bank_name)
    MyOnclickEditText et_bank_name;//银行名称
    @Bind(R.id.et_bank_account)
    MyOnclickEditText et_bank_account;//银行帐号
    @Bind(R.id.rl_salesman)
    RelativeLayout rl_salesman;//所属业务员
    @Bind(R.id.rl_filiale)
    RelativeLayout rl_filiale;//所属分公司
    @Bind(R.id.rl_status)
    RelativeLayout rl_status;//所属类型
    @Bind(R.id.et_cu_category)
    MyOnclickEditText et_cu_category;//所属类型


    private String mCId;//客户id
    private CustomerDataBean data = new CustomerDataBean();
    private Calendar calendar;

    private String urlSave = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_customermanage_details;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mCId = getIntent().getStringExtra("id");
        iv_top_left.setVisibility(View.VISIBLE);
        iv_top_left.setOnClickListener(this);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("客户管理详情");
        iv_top_right.setVisibility(View.VISIBLE);
        iv_top_right_left.setVisibility(View.VISIBLE);
        iv_top_right_left.setImageResource(R.drawable.delete);
        iv_top_right_left.setOnClickListener(this);
        iv_top_right.setImageResource(R.drawable.bianji);
        iv_top_right.setOnClickListener(this);
        tv_top_right.setText("保存");
        tv_top_right.setOnClickListener(this);
//        et_address.setOnClickListener(this);
        et_sex.setEnabled(false);
        et_birthday.setEnabled(false);
        et_document_type.setEnabled(false);
        et_marital_status.setEnabled(false);
        et_sex.setOnClickListener(this);
        et_birthday.setOnClickListener(this);
        et_document_type.setOnClickListener(this);
        et_marital_status.setOnClickListener(this);
        et_cu_category.setOnClickListener(this);
        calendar = Calendar.getInstance();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.et_cu_category://类型
                new SlideListDialog(mContext, new String[]{"意向", "成交"}) {
                    @Override
                    public void sureData(String value, int selectIndex) {
                        selectIndex++;
                        data.setCu_category(selectIndex + "");
                        et_cu_category.setText(value);
                    }
                }.show();
                break;
            case R.id.et_marital_status://婚姻类型
                new SlideListDialog(mContext, new String[]{"已婚", "未婚"}) {
                    @Override
                    public void sureData(String value, int selectIndex) {
                        selectIndex++;
                        data.setMarital_status(selectIndex + "");
                        et_marital_status.setText(value);
                    }
                }.show();
                break;
            case R.id.et_document_type://证件类型
                new SlideListDialog(mContext, new String[]{"身份证", "护照"}) {
                    @Override
                    public void sureData(String value, int selectIndex) {
                        selectIndex++;
                        data.setDocument_type(selectIndex + "");
                        et_document_type.setText(value);
                    }
                }.show();
                break;
            case R.id.et_birthday://选择生日
                new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        // TODO Auto-generated method stub
                        //更新EditText控件日期 小于10加0
                        et_birthday.setText(year + "-" + (month + 1) + "-" + day);
                        data.setBirthday(year + "-" + (month + 1) + "-" + day);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH))
                        .show();
                break;
            case R.id.et_sex://性别
                new SlideListDialog(mContext, new String[]{"男", "女"}) {
                    @Override
                    public void sureData(String value, int selectIndex) {
                        selectIndex++;
                        data.setSex(selectIndex + "");
                        et_sex.setText(value);
                    }
                }.show();
                break;
            case R.id.iv_top_right://编辑
                ToastUtils.getInstance().showToast("编辑");
                changeViewStatus(1);
                break;
            case R.id.tv_top_right://保存
                saveData();
                break;
            case R.id.iv_top_right_left://删除
                delData();
                break;
        }
    }

    /**
     * 修改显示格式
     *
     * @param type 1.编辑状态   2.不可编辑状态
     */
    private void changeViewStatus(int type) {
        if (type == 1) {
            et_sex.setEnabled(true);
            et_document_type.setEnabled(true);
            et_marital_status.setEnabled(true);
            et_birthday.setEnabled(true);
            rl_salesman.setVisibility(View.GONE);
            rl_status.setVisibility(View.GONE);
            rl_filiale.setVisibility(View.GONE);
//            rl_address.setVisibility(View.VISIBLE);
            iv_top_right.setVisibility(View.GONE);
            tv_top_right.setVisibility(View.VISIBLE);
            if (mCId == null || "".equals(mCId)) {
                rl_status.setVisibility(View.VISIBLE);
            }
            et_name.setFocusableAndGravity(true, Gravity.LEFT);//姓名
            et_sex.setFocusableAndGravity(false, Gravity.LEFT);//性别
            et_birthday.setFocusableAndGravity(false, Gravity.LEFT);//生日
            et_cu_category.setFocusableAndGravity(false, Gravity.LEFT);//生日
            et_document_type.setFocusableAndGravity(false, Gravity.LEFT);//证件类型
            et_document_number.setFocusableAndGravity(true, Gravity.LEFT);//证件号码
            et_marital_status.setFocusableAndGravity(false, Gravity.LEFT);//婚姻状态
            et_work_unit.setFocusableAndGravity(true, Gravity.LEFT);//工作单位
            et_phone.setFocusableAndGravity(true, Gravity.LEFT);//手机号
            et_email.setFocusableAndGravity(true, Gravity.LEFT);//电子邮箱
            et_zip_code.setFocusableAndGravity(true, Gravity.LEFT);//邮政编码
            et_address.setFocusableAndGravity(false, Gravity.LEFT);//联系地址
            et_address_details.setFocusableAndGravity(true, Gravity.LEFT);//详细地址
            et_bank_name.setFocusableAndGravity(true, Gravity.LEFT);//银行名称
            et_bank_account.setFocusableAndGravity(true, Gravity.LEFT);//银行账号
        } else {
            et_sex.setEnabled(false);
            et_document_type.setEnabled(false);
            et_marital_status.setEnabled(false);
            et_birthday.setEnabled(false);
            rl_salesman.setVisibility(View.VISIBLE);
            rl_status.setVisibility(View.VISIBLE);
            rl_filiale.setVisibility(View.VISIBLE);
//            rl_address.setVisibility(View.GONE);
            iv_top_right.setVisibility(View.VISIBLE);
            tv_top_right.setVisibility(View.GONE);
            et_name.setFocusableAndGravity(false, Gravity.RIGHT);//姓名
            et_sex.setFocusableAndGravity(false, Gravity.RIGHT);//性别
            et_cu_category.setFocusableAndGravity(false, Gravity.RIGHT);//性别
            et_birthday.setFocusableAndGravity(false, Gravity.RIGHT);//生日
            et_document_type.setFocusableAndGravity(false, Gravity.RIGHT);//证件类型
            et_document_number.setFocusableAndGravity(false, Gravity.RIGHT);//证件号码
            et_marital_status.setFocusableAndGravity(false, Gravity.RIGHT);//婚姻状态
            et_work_unit.setFocusableAndGravity(false, Gravity.RIGHT);//工作单位
            et_phone.setFocusableAndGravity(false, Gravity.RIGHT);//手机号
            et_email.setFocusableAndGravity(false, Gravity.RIGHT);//电子邮箱
            et_zip_code.setFocusableAndGravity(false, Gravity.RIGHT);//邮政编码
            et_address_details.setFocusableAndGravity(false, Gravity.RIGHT);//详细地址
            et_bank_name.setFocusableAndGravity(false, Gravity.RIGHT);//银行名称
            et_bank_account.setFocusableAndGravity(false, Gravity.RIGHT);//银行账号
        }
    }

    @Override
    public void createLoadData() {
        super.createLoadData();
        if (mCId == null || "".equals(mCId)) {
            changeViewStatus(1);
            tv_top_right.setText("添加");
            iv_top_right_left.setVisibility(View.GONE);
            urlSave = URL.getUrl().ADDCUSTOMER;
            return;
        }
        urlSave = URL.getUrl().SAVECUSTOMER;
        OkGo.<String>post(URL.getUrl().GETCUSTOMERDETAILS)
                .tag(this)
                .params("c_id", mCId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        CustomerDetailsBean bean = MyApplication.getInstance().getBeanForJson(CustomerDetailsBean.class, response.body());
                        if (bean == null) {
                            return;
                        }
                        if (bean.getCode() == 1 && bean.getData() != null) {
                            data = bean.getData();
                            bindData();
                        }
                    }
                });
    }

    /**
     * 绑定数据
     */
    private void bindData() {
        et_name.setText(data.getC_name());
        et_sex.setText("2".equals(data.getSex()) ? "女" : "男");
        et_birthday.setText(data.getBirthday());
        et_document_type.setText("1".equals(data.getDocument_type()) ? "身份证" : "护照");
        et_document_number.setText(data.getDocument_number());
        et_marital_status.setText("1".equals(data.getMarital_status()) ? "已婚" : "未婚");
        et_work_unit.setText(data.getWork_unit());
        et_phone.setText(data.getPhone());
        et_email.setText(data.getEmail());
        et_address_details.setText(data.getAddress());
        et_zip_code.setText(data.getZip_code());
        et_bank_name.setText(data.getBank_name());
        et_bank_account.setText(data.getBank_account());

    }

    /**
     * 保存
     */
    private void saveData() {
        OkGo.<String>post(urlSave)
                .params("m_number", MyApplication.getInstance().bean.getM_number())
                .params("c_id", data.getC_id())
                .params("name", et_name.getText().toString().trim())
                .params("c_name", et_name.getText().toString().trim())
                .params("sex", data.getSex())
                .params("birthday", data.getBirthday())
                .params("document_type", data.getDocument_type())
                .params("document_number", et_document_number.getText().toString())
                .params("marital_status", data.getMarital_status())
                .params("work_unit", et_work_unit.getText().toString().trim())
                .params("phone", et_phone.getText().toString())
                .params("email", et_email.getText().toString().trim())
                .params("cu_category", data.getCu_category())
                .params("zip_code", et_zip_code.getText().toString().trim())
                .params("address", et_address_details.getText().toString().trim())
                .params("bank_name", et_bank_name.getText().toString().trim())
                .params("bank_account", et_bank_account.getText().toString().trim())
                .execute(new MyStringCallBack<BaseBean>(tv_top_right) {
                    @Override
                    public void onSuccessData(BaseBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getCode() == 1) {
                            if (mCId == null || "".equals(mCId)) {
                                finish();
                            } else {
                                changeViewStatus(2);
                            }
                        }
                        ToastUtils.getInstance().showToast(bean.getMsg());
                    }
                });
    }

    private void delData() {
        OkGo.<String>post(URL.getUrl().DELCUSTOMER)
                .params("c_id", data.getC_id())
                .execute(new MyStringCallBack<BaseBean>(iv_top_right_left) {
                    @Override
                    public void onSuccessData(BaseBean bean) {
                        super.onSuccessData(bean);
                        ToastUtils.getInstance().showToast(bean.getMsg());
                        if (bean.getCode() == 1) {
                            finish();
                        }

                    }
                });
    }
}
